import { getDetailAPI, rechangeAPI } from '@/apis/card'
//@ts-ignore
import DateUtils from '@/utils/dateUtils';
/**
 * 续费 recharge
 * 约定：
 * data,methods ,computed里面变量和方法名统统以recharge开头
 */
export default {
  data() {
    return {
      rechargeDialogFormData: {
        type: 'recharge',
        id: null,
        dialogVisible: false,
        title: '月卡续费',
        form: {
          personName: '', // 车主姓名
          phoneNumber: '', // 联系方式
          carNumber: '', // 车牌号码
          carBrand: '', // 车辆品牌
          payTime: [],
          paymentAmount: '',
          paymentMethod: '',
        },
      },
      rechargeFormItems: {
        personName: {
          disabled: true
        },
        phoneNumber: {
          disabled: true
        },
        carNumber: {
          disabled: true
        },
        carBrand: {
          disabled: true
        }
      }
    }
  },
  beforeCreate() {
    this.readOnlyList = ['personName', 'phoneNumber', 'carNumber', 'carBrand']
  },
  methods: {
    async rechargeGetDetail(id) {
      this.rechargeDialogFormData.id = id
      const r = await getDetailAPI(id)
      let { form } = this.rechargeDialogFormData
      this.readOnlyList.forEach(key => form[key] = r.data[key])
      form.paymentMethod = r.data.paymentMethod

      this.rechargeDialogFormData.dialogVisible = true
    },
    rechargeResetFrom() {
      const form = this.rechargeDialogFormData.form
      form.payTime = []
      form.paymentAmount = ''
      form.paymentMethod = ''

    },
    async rechargeComfirmForm() {
      const { form, id } = this.rechargeDialogFormData
      const params = {}
      params.carInfoId = id
      params.paymentAmount = form.paymentAmount
      params.paymentMethod = form.paymentMethod
      params.cardStartDate = DateUtils.dataFormat(form.payTime[0])
      params.cardEndDate = DateUtils.dataFormat(form.payTime[1])
      try {
        const r = await rechangeAPI(params)
        this.$message.success('续费成功')
        form.payTime = []
        form.paymentAmount = ''
        form.paymentMethod = ''
        this.fetchTableData()
      } catch (error) {
        this.$message.error(error.msg || '续费失败')

      }

    }
  },
  mounted() {
  }
}